<?php
/**
 * Created by PhpStorm.
 * User: 96572
 * Date: 2017/5/7
 * Time: 11:13
 */

namespace app\modules\admin\controllers;

use app\modules\admin\models\Menu;
use Yii;
use app\common\CommonController;
use app\modules\admin\models\Role;
use yii\helpers\Url;
use yii\web\Response;

class RoleController extends CommonController
{
    public function actionCreate()
    {
        $role = new Role(['scenario'=>Role::INSERT_SCENARIO]);
        $request = Yii::$app->request;
        $role->load($request->post());
        if ($request->getIsAjax())
        {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return $role->validate();
        }
        elseif ($request->getIsPost())
        {
            if ($role->save())
            {
                $this->success(
                    '添加成功',
                    '添加成功',
                    [
                        [
                            'name'=>'角色列表',
                            'url'=>Url::toRoute('list')
                        ],
                        [
                            'name'=>'继续添加',
                            'url'=>Url::toRoute('create')
                        ],
                        [
                            'name'=>'修改',
                            'url'=>Url::toRoute(['edit','role_name'=>$role->name])
                        ]
                    ]
                );
            }
            else
            {
                $this->error(
                    '添加失败',
                    current(current($role->getFirstErrors())),
                    [
                        [
                            'name'=>'重新添加',
                            'url'=>Url::toRoute('create')
                        ]
                    ]
                );
            }
        }
        else
        {
            return $this->render(
                'create',
                [
                    'role'=>$role,
                ]
            );
        }
    }

    public function actionList()
    {
        $roles = Role::GetRoles();
        return $this->render(
            'list',
            [
                'roles'=>$roles
            ]
        );
    }

    public function actionUpdatePermission($name)
    {
        $auth = Yii::$app->authManager;
        $permissions = $auth->getPermissions();
        $rolePermissions = $auth->getPermissionsByRole($name);
        if ($rolePermissions) {
            $rolePermissions = array_column($rolePermissions,'name');
        }
        $request = Yii::$app->request;
        if ($request->getIsGet()) {
            $menu = Menu::getMenuTree();
            return $this->render(
                'update-permission',
                [
                    'menus'=>$menu,
                    'rolePermissions'=>$rolePermissions,
                ]
            );
        }
        $permissionNames = $request->post('name',[]);
        $originalPermissionNames = $request->post('original_name',[]);

        $addNames = array_filter($permissionNames,function ($val) use($originalPermissionNames){
            return !in_array($val,$originalPermissionNames);
        });
        $removeNames = array_filter($originalPermissionNames,function ($val) use($permissionNames){
            return !in_array($val,$permissionNames);
        });

        $role = $auth->getRole($name);
        $permissions = [];
        if ($addNames) {
            foreach ($addNames as $permissionName) {
                if (!isset($permissions[$permissionName])) {
                    $permissions[$permissionName] = $auth->getPermission($permissionName);
                    $auth->addChild($role,$permissions[$permissionName]);
                }
            }
        }

        $permissions = [];
        if ($removeNames) {
            foreach ($removeNames as $permissionName) {
                if (!isset($permissions[$permissionName])) {
                    $permissions[$permissionName] = $auth->getPermission($permissionName);
                    $auth->removeChild($role,$permissions[$permissionName]);
                }
            }
        }

        $this->success(
            '更新成功',
            '更新成功',
            [
                [
                    'name'=>'角色列表',
                    'url'=>Url::toRoute('admin/role')
                ],
                [
                    'name'=>'继续修改',
                    'url'=>Url::toRoute([
                        'update-permission',
                        'name'=>$name
                    ])
                ]
            ]
        );

    }
}